package com.killer.edu.auth.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.killer.edu.auth.dto.MenuDTO;
import com.killer.edu.auth.dto.MenuNodeDTO;
import com.killer.edu.auth.entity.Menu;
import com.killer.edu.auth.service.IMenuService;
import com.killer.edu.common.entity.vo.Result;
import com.killer.edu.common.result.ResponseDTO;
import com.killer.edu.common.util.ConverUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author killer
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @PostMapping(value = "/saveOrUpdate")
    public ResponseDTO saveOrUpdate(@RequestBody MenuDTO menuDTO) {
        Menu menu = ConverUtil.convert(menuDTO, Menu.class);
        menu.setUpdatedTime(new Date());
        if (menu.getParentId() == -1) {
            menu.setLevel(0);
        } else {
            Menu parentMenu = menuService.getById(menu.getParentId());
            menu.setLevel(parentMenu.getLevel() + 1);
        }
        return ResponseDTO.success(menuService.saveOrUpdate(menu));
    }

    @GetMapping("/{id}")
    public ResponseDTO<MenuDTO> getById(@PathVariable Integer id) {
        MenuDTO menuDTO = ConverUtil.convert(menuService.getById(id), MenuDTO.class);
        return ResponseDTO.success(menuDTO);
    }

    @DeleteMapping(value = "/{id}")
    public ResponseDTO<Boolean> delete(@PathVariable Integer id) {
        return ResponseDTO.success(menuService.deleteWithAssociation(id));
    }


    @GetMapping("/getAll")
    public ResponseDTO<List<MenuDTO>> getAll() {
        return ResponseDTO.success(ConverUtil.convertList(menuService.list(), MenuDTO.class));
    }

    @PostMapping("/getMenuPages")
    public ResponseDTO<Page<MenuDTO>> getMenuPages(@RequestBody MenuQueryParam menuQueryParam) {
        log.info("Get menu list with paging. params:{}", menuQueryParam);
        Page<Menu> selectedPage = menuService.getMenuPages(menuQueryParam);
        Page<MenuDTO> menuDTOPage = new Page<>();
        BeanUtils.copyProperties(selectedPage, menuDTOPage);
        if (CollectionUtils.isNotEmpty(selectedPage.getRecords())) {
            List<MenuDTO> menuDTOList = selectedPage.getRecords().stream()
                    .map(menu -> ConverUtil.convert(menu, MenuDTO.class)).collect(Collectors.toList());
            menuDTOPage.setRecords(menuDTOList);
        }
        return ResponseDTO.success(menuDTOPage);
    }

    @PostMapping("/switchShown")
    public ResponseDTO<Boolean> switchShown(@RequestBody MenuDTO menuDTO) {
        return ResponseDTO.success(menuService.switchShown(ConverUtil.convert(menuDTO, Menu.class)));
    }

    @GetMapping("/getMenuNodeList")
    public ResponseDTO<List<MenuNodeDTO>> getMenuNodeList() {
        return ResponseDTO.success(menuService.getMenuNodeList());
    }

    @GetMapping("/getByRoleId")
    ResponseDTO<List<MenuDTO>> getByRoleId(@RequestParam("roleId") Integer roleId) {
        return ResponseDTO.success(ConverUtil.convertList(menuService.getByRoleIdIgnoreIsShown(roleId), MenuDTO.class));
    }

    @PostMapping("/allocateRoleMenus")
    ResponseDTO<Boolean> allocateRoleMenus(@RequestBody AllocateRoleMenuDTO allocateRoleMenuDTO) {
        log.info("Allocate role menus with params:{}", allocateRoleMenuDTO);
        menuService.allocateRoleMenus(allocateRoleMenuDTO);
        return ResponseDTO.success(Boolean.TRUE);
    }
}
